Ext.ns('cn.hls.web.action.biz.car');
Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if (index > -1) {
	this.splice(index, 1);
	}
	};
var emergencyForm = cn.hls.web.action.biz.car.emergencyForm;
 emergencyForm = function() {
	var uploadPanel,uploadWin;
   return { 
      init : function() {
    	this.initRender();
      },
      /**附件上传**/
      ajaxFileUpload:function(){
    	  var filepath =$("#attachFile").val();
    		 var extStart = filepath.lastIndexOf(".");
    	     var ext = filepath.substring(extStart, filepath.length).toUpperCase();
    	     if (ext  == ".exe"){
    	    	 Ext.Mytip.msg('提示','文件格式不正确!');
    	    	 return;
    	     }else{
    	    	 var size=document.getElementById("attachFile").files[0].size;
    	    	 if(size>10*1024*1024){//最大不能超过10M
    	    		 Ext.Mytip.msg('提示','上传文件超过规定10M大小!');
    	    		 return;
    	    	 }
    	     }
    	     $.ajaxFileUpload({  
    	         url: cfg.cxt+'/upload/uploadFile.action', 
    	         secureuri: false,
    	         fileElementId: 'attachFile',  
    	         dataType: 'json', //这里修改为content  
    	         success: function (data) {
    	        	 if(data.status==200){
    	        	   Ext.Mytip.msg('提示','文件传成功!');
    	        	   var files=$("#hid_filePath").val();
    	        	   if(files==''){
    	        		   $("#hid_filePath").val(data.info);
    	        	   }else{
    	        		   files+=','+data.info;
    	        		   $("#hid_filePath").val(files);//重新设置值
    	        	   }
    	        	   var attachDiv=$("#attach_div");
    	        	   var fileName=data.info.substring(data.info.indexOf("/")+1);
    	        	   var pre_fileName=fileName.substring(0,fileName.indexOf("."));
    	        	   attachDiv.append('<span id="sp_'+pre_fileName+'" style="padding-left:8px;padding-right:5px;">'+fileName+'</span><a id="a_'+pre_fileName+'" href="javascript:void(0);" onclick=emergencyForm.delFile("'+fileName+'");>删除</a>'
    	        			   			+'<input type="hidden" id="hid_'+pre_fileName+'" value="'+data.info+'"/>');
    	        	   $("#attachFile").hide();
    	        	   $("#attachFile").val('');//清空上传中的文件
						uploadWin.hide();
    	        	   
    	           }else{
    	        	   Ext.Mytip.msg('提示','文件上传失败!');
    	           }
    	         },  
    	         error: function (data, status, e) {  
    	        	resultFlag=false;
    	        	console.log(e);
    	        	console.log("status:"+status);
    	        	console.log(data);
    	        	Ext.Mytip.msg('提示','文件上传失败，原因:'+e);
    	         }  
    	       }  
    	     )
      },
      /**
       * 删除文件
       */
      delFile:function(fileName){
    	  var element=fileName.substring(0,fileName.indexOf("."));
    	  Ext.Msg.confirm('请确认',
					'<span style="color:red"><b>提示：</b>确定删除吗？</span>',
					function(btn, text) {
						if (btn == 'yes') {
							var fileName=$("#hid_"+element).val();
							Ext.Ajax.request({
								url : cfg.cxt + '/upload/delFile.action',
								success : function(response) {
									var rt = Ext.decode(response.responseText);
									if (rt.status==200) {
										Ext.Mytip.msg('提示', '删除成功！');
										$("#sp_"+element).remove();
										$("#hid_"+element).remove();
										$("#a_"+element).remove();
										//更新hid_filePath的内容
										var files=$("#hid_filePath").val();
										if($.trim(files)!=''){
											var a=files.split(',');
											if(a.length>1){
												a.remove(fileName);
												$("#hid_filePath").val(a.join(','));
											}
										}
									} else {
										bangni.ajaxError(response);
									}
								},
								failure : bangni.ajaxError,
								params : {
									"fileName":fileName
								}
							});
						}
					});
      },
  
      /**打开附件上传窗口**/
      openAttachWin:function(){
    	if(!uploadWin){
    		uploadWin=emergencyForm.createUploadWin();
    	}
    	$("#attachFile").show();
    	uploadWin.show();//打开窗口
      },
      //创建上传窗口
      createUploadWin:function(){
    	  if(!uploadPanel){
    		  uploadPanel=emergencyForm.createUploadPanel();
    	  }
    	  return new ExtWin({
  			width : 300,
				height : 150,
				title : '<div class="panelTitleCss">附件上传</div>',
  		  	items:[uploadPanel],
  		  	buttons : [{
					text : '上传',
					id:"btn_Save",
					iconCls : 'acceptIcon',
					handler : function() {
						emergencyForm.ajaxFileUpload();
					}
  		  	},{
					text : '取消',
					iconCls : 'deleteIcon',
					handler : function() {
						$("#attachFile").hide();
						uploadWin.hide();
					}
				}]
  		  });
      },
      createUploadPanel:function(){
    	  uploadPanel=new Ext.Panel({
    		  layout : 'fit',
    		  items: [{
		        	padding : 5,
					bodyStyle:'overflow-y:auto;overflow-x:hidden;',
					contentEl : 'attachFile'
		        }]
    	  });
    	  return uploadPanel;  
      },
      initRender: function(){
    	//应急预案日期日历控件渲染组件
    	  var emergencyTime=Ext.get('hid_emergencyTime').dom.value;//应急预案日期
    	  new Ext.form.DateField({
    			id : 'dtemergencyTime',
    			width : 180,
    			editable : true,
    			format : "Y-m-d",
    			name : 'emergencyTime',
    			renderTo : 'sp_emergencyTime',
    			allowBlank : false,
    			value:Ext.isEmpty(emergencyTime)?null:new Date(emergencyTime.substr(0,10))
    		});
      }
   }
}();
Ext.onReady(emergencyForm.init, emergencyForm);


 